/** 修改为ees6

const express = require('express');
const cookie = require('cookie-parser');
const multer = require('multer');
const loader = require('./loader');
*/
// 修改为 es6

import express from 'express';
// const { e: express } = e;
import cookie from 'cookie-parser';
// const {cookieParser: cookie} = cookieParser;
import multer from 'multer';
import { pathMap as loader } from './api.js'
import bodyParser from "body-parser";


let app = new express();

/**
 * 配置跨域
 */
app.use((req, res, next)=>{
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Request-Methods", "PUT, DELETE, GET, POST, OPTION");
  res.setHeader("Access-Control-Allow-Headers", "content-type, x-requested-with");
  next();
})

/**
 * 静态文件的文件夹所走的路径
 */

// app.use(express.static('./pages'));

/**
 * cookies中间件
 * 获取cookies：req.cookies
 * 写入cookie：req.cookies('token','xxxxxxx');
 */
app.use(cookie());

/**
 * 用来接收post请求的参数 获取参数 req.body
 */
app.use(express.json());

/**
 * 拦截器
 */
app.get('/api/*', function (req, res, next) {
  // 获取cookie
  const cookies = req.cookies;
  console.log(cookies);
  // 写入cookie
  // rea.cookies('token','dsadsadada')
  next();
  if (cookies.token) {
    if (cookies.token) {
      next(); // 放行
    } else {
      // 重定向
      res.redirect('/login.html');
    }
  }
});

// const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);
// let filePath = path.join(__dirname, "html");
// let urlBodyParser = bodyParser.urlencoded({ extended: false });

// app.get("/logon", function (req, res) {
//   let name = req.query.userName;
//   let password = req.query.passwd;
//   console.log(name + "\t" + password);
//   res.send(name + "&nbsp;&nbsp;" + password);
// });


// app.post("/logon", urlBodyParser, async function (req, res) {
//   let name = req.body.userName;
//   let password = req.body.passwd;
//   if (name === "admin" && password === "java") {
//     res.sendFile(path.join(__dirname, "pages/index.html"));
//     return 0;
//   } else {
//     res.sendFile(path.join(__dirname, "pages/erro.html"));
//   }


// });


/**
 * 路由
 */
app.get('/api/getAllStudent', loader.get('/getAllStudent'));
app.get('/api/getBarChatData', loader.get('/getBarChatData'));
app.get('/api/getDeviceList', loader.get('/getDeviceList'));
app.get('/api/getDeviceMeteTable', loader.get('/getDeviceMeteTable'));
app.get('/api/getLineData', loader.get('/getLineData'));
app.get('/api/getDeviceCountUsed', loader.get('/getDeviceCountUsed'));
app.get('/api/getDeviceCountOFF', loader.get('/getDeviceCountOFF'));
app.get('/api/getDeviceCountON',loader.get('/getDeviceCountON'));
app.get('/api/getPowerList', loader.get('/getPowerList'));
app.get('/api/getTemplateList', loader.get('/getTemplateList'));
app.get('/api/getHumidnessList', loader.get('/getHumidnessList'));
app.get('/api/getPieData2', loader.get('/getPieData2'));
app.get('/api/getPieData1', loader.get('/getPieData1'));
app.get('/api/getElectricityList', loader.get('/getElectricityList'));
app.get('/api/getJobList', loader.get('/getJobList'));
// app.get('/api/getJobList', loader.get('/getJobList'));

/**
 * lijiang route
 */

app.get('/api/carTable',loader.get('/carTable'));




app.listen(
  '12306', '0.0.0.0',
  () => console.log(`Example app listening on  port !`)
);
